﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    internal static partial class EWHelpers
    {
        public static class TypeHelpers
        {
            public static IEnumerable<Type> GetSelfAndBaseTypes(Type type)
            {
                EWDebug.IsNotNull(type, "type");

                while (type != null)
                {
                    yield return type;
                    type = type.BaseType;
                }
            }

            public static bool IsDynamicObject(Type type)
            {
                EWDebug.IsNotNull(type, "type");

                return type == typeof(object) || typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);
            }

            public static T Create<T>()
            {
                return (T)Create(typeof(T));
            }

            public static object Create(Type type)
            {
                EWDebug.IsNotNull(type, "type");

                return Expression.Lambda<Func<object>>(Expression.New(type), new ParameterExpression[0]).Compile()();
            }
        }
    }
}
